import { getMaterialList } from '../../api/index'

Component({
  properties: {
    reachBottomTime: {
      type: Number,
      value: 0
    },
    onshowTime: {
      type: Number,
      value: 0
    },
    material: {
      type: Object,
      value: {
        currentPage: 1,
        isEmpty: false,
        list: []
      }
    }
  },
  observers: {
    reachBottomTime (val) {
      if (val) this.loadMore()
    },
    // onshowTime (val) {
    //   if (val && !this.data.isFirst.material) {
    //     this.getList()
    //   }
    // }
  },
  data: {
    currentPage: 1,
    pageSize: 15,
    list: [],
    isEmpty: false
  },
  methods: {
    async loadMore () {
      try {
        if (this.loading || this.alloaded) return
        this.loading = true
        const { ResultSet } = await getMaterialList({ currentPage: this.data.material.currentPage, pageSize: this.data.pageSize })
        this.alloaded = !ResultSet.userData.data.length
        this.data.material.isEmpty = !ResultSet.userData.data.length
        this.setData({ 'material.isEmpty': this.data.material.isEmpty })
        if (ResultSet.userData.data.length) {
          this.data.material.list.push(...ResultSet.userData.data)
          this.setData({
            'material.currentPage': this.data.material.currentPage + 1,
            'material.list': this.data.material.list
          })
        }
        this.triggerEvent('change', this.data.material)
        this.loading = false
      } catch (err) {
        this.setData({ 'material.isEmpty': true })
        this.loading = false
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    },
    // async getList () {
    //   try {
    //     const { ResultSet } = await getMaterialList({ currentPage: 1, pageSize: this.data.pageSize })
    //     if (ResultSet.userData.data.length) this.data.currentPage = 2
    //     this.data.isEmpty = !ResultSet.userData.data.length
    //     this.setData({
    //       list: ResultSet.userData.data,
    //       currentPage: this.data.currentPage,
    //       isEmpty: this.data.isEmpty
    //     })
    //     this.triggerEvent('changeFirstStatus', 'material')
    //   } catch (err) {
    //     wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    //   }
    // }
  },
  // created () {
  //   this.getList()
  // }
})